package com.bocai.kelalacar.ui.car.main.presenter;

import android.util.Log;

import com.bocai.kelalacar.base.BaseResponse;
import com.bocai.kelalacar.bean.MessageBean;
import com.bocai.kelalacar.net.Api;
import com.bocai.kelalacar.ui.car.main.contract.MessageContract;
import com.bocai.kelalacar.util.AESUtils;
import com.bocai.kelalacar.util.RetryWithDelay;
import com.google.gson.Gson;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/7/8.
 */

public class MessagePresenter extends MessageContract.presenter{

    private final MessageContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public MessagePresenter(MessageContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void messageList(String pageIndex, String pageSize) {
        api.messageList(pageIndex,pageSize).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    MessageBean bean = new Gson().fromJson(result,MessageBean.class);
                    view.getMessageList(bean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void readMessage(String messageId) {
        api.readMessage(messageId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
//                    String result = AESUtils.decode(baseResponse.getData());
//                    Log.e("result",result);
                    view.readMessage();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void deleteMessage(String messageId, final int position) {
        api.deleteMessage(messageId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
//                    String result = AESUtils.decode(baseResponse.getData());
//                    Log.e("result",result);
                    view.deleteMessage(position);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
